package torres.item;

import java.io.File;
import java.util.Scanner;
import torres.map.Location;
import torres.map.World;

/**
 * @author Torres
 * Reads in a file to instantiate all items from file.
 */
public class ItemGenerator {
	
	/**
	 * Reads in and instantiates all possible items from an item file onto map.
	 * @param file File designating where items will be
	 */
	public static void generate(File file) {
		
		Scanner reader = null;
		World world = World.getInstance();
		
		try {
			
			reader = new Scanner(file);
			int row = reader.nextInt();
			int column = reader.nextInt();
			
			int line = 0;
			ItemManager itemManager = ItemManager.getInstance();
			while(line < row) {
				for(int col = 0; col < column; col++) {
					Item item;
					if(!reader.hasNextInt()) {
						reader.next();
						continue;
					}
					int itemID = reader.nextInt();
					if(itemID < 200) { //all Takeable IDs are less than this value
						item = itemManager.getTakeableItem(itemID);
						world.addItem(item, new Location(col, line));
					}
					else if(itemID < 300) { //all Equippable IDs are less than this value
						item = itemManager.getEquippableItem(itemID);
						world.addItem(item, new Location(col, line));
					}
					else if(itemID < 400) { //all Interactive IDs are less than this value
						item = itemManager.getInteractiveItem(itemID);
						world.addItem(item, new Location(col, line));
					}
					else if(itemID < 500) { //all OneShot IDs are less than this value
						TrapItem trap = itemManager.getTrapItem(itemID);
						world.addTrap(trap, new Location(col, line));
					}
					else if(itemID < 600) { 
						item = itemManager.getOneShotItem(itemID);
						world.addItem(item, new Location(col, line));
					}
					else if(itemID < 700) {
						item = itemManager.getWeapon(itemID);
						world.addItem(item, new Location(col, line));
					}
					else {	//it must be an Obstacle
						ObstacleItem obstacle = itemManager.getObstacleItem(itemID);
						world.addObstacle(obstacle, new Location(col, line));
					}
				}
				line++;
			}
			//reader.next();
		}
		catch(Exception e) {
			
			e.printStackTrace();
		}
	}
}
